home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / rukc10.zip / X01.C < prev    next >
C/C++ Source or Header  |  1993-03-06  |  9KB  |  299 lines

  1.  
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7.  
  8. #include "ruckdac.h"
  9.  
  10. /*
  11. X01.c 28-Feb-93 chh
  12. Load & Play VOC/WAV file
  13. */
  14.  
  15. /*
  16. The following structures are in ruckdac.h
  17. */
  18.  
  19. extern struct DacDataArea __pascal DACDATA;
  20.  
  21. struct SysInfoPack SIP;
  22. struct InitPack IP;
  23. struct XitPack XP;
  24. struct LoadPack LP;
  25. struct SetPack SP;
  26. struct SetProPack SPP;
  27. struct PlaybackPack PBP;
  28. struct GetDataPack GDP;
  29. struct DeallocPack DP;
  30.  
  31.  
  32. int rez, rez2;      /* result status codes */
  33. char nums[9] = {7}; /* number buffer for _cgets()*/
  34. char filename[81];  /* pathname to load */
  35.  
  36.  
  37. int pick_device(int *devID, int *XMSflag)
  38. {
  39.  
  40.     int td=0;
  41.  
  42.     SIP.Func = SysInfoDac;
  43.     rez = RUCKDAC(&SIP);
  44.     if (rez == 0) {
  45.         printf("CPU is a %u/%u\n",SIP.CPU,SIP.MHz);
  46.  
  47.         printf("\n0. End program");
  48.         printf("\n1. PC speaker at port 42h");
  49.         if (SIP.SD[1].device)
  50.             printf("\n2. LPT-DAC on LPT1, port %xh",SIP.SD[1].Port);
  51.         if (SIP.SD[2].device)
  52.             printf("\n3. Disney Sound Source port %xh",SIP.SD[2].Port);
  53.         if (SIP.SD[3].device)
  54.             printf("\n\n4. AdLib Music Synthesizer Card, port %xh",SIP.SD[3].Port);
  55.         if (SIP.SD[4].device)
  56.             printf("\n5. Sound Blaster, port %xh",SIP.SD[4].Port);
  57.         if (SIP.SD[5].device)
  58.             printf("\n6. Sound Blaster Pro, port %xh",SIP.SD[5].Port);
  59.         if (SIP.SD[4].device)
  60.             printf("\n\n7. Sound Blaster as 5 but use XMS (if applicable)");
  61.         if (SIP.SD[5].device)
  62.             printf("\n8. Sound Blaster Pro as 6 but use XMS (if applicable)\n");
  63.  
  64.         printf("\nSelection: ");
  65.         td = atoi(_cgets(nums));
  66.         td--;   /* since devices are numbered 0 to 5 */
  67.  
  68.         if (td > 6) {
  69.             *XMSflag = 1;       /* XMS memory selected with SB */
  70.             td-=2;              /* map to appropriate device */
  71.         }
  72.  
  73.         if ((td >=0) && (td <=5)) { /* validate device selected available */
  74.             if (SIP.SD[td].device == 0)
  75.                 td = -1;
  76.         }
  77.         else
  78.             td = -1;
  79.     }
  80.  
  81.     *devID = td;
  82.     return(rez);
  83. }
  84.  
  85.  
  86. int init_device(int devID)
  87. {
  88.     /*
  89.     Initialize RUCKDAC and device and register ExitDac with _atexit
  90.     The IP.port for devices 0 and 3 are set to 0 for low-rez mode,
  91.     or their respective actual ports for hi-rez mode (0x42 and 0x388)
  92.     */
  93.  
  94.     IP.Func = InitDac;
  95.     IP.DeviceID = devID;
  96.     IP.IOport = SIP.SD[devID].Port;
  97.     IP.IRQline = SIP.SD[devID].IRQ;
  98.     IP.DMAch = SIP.SD[devID].DMA;
  99.  
  100.     if ((devID == 0) || (devID == 3))   /* use low-rez mode for */
  101.         IP.IOport = 0;                  /* PC speaker and Adlib */
  102.  
  103.     rez = RUCKDAC(&IP);                 /* Initialize */
  104.     if (rez == 0) {
  105.  
  106.         XP.Func = AtExitDac;
  107.         rez2 = RUCKDAC(&XP);
  108.         if (rez2 != 0) {
  109.             printf("AtExitDac failed, press Enter to continue");
  110.             getchar();
  111.         }
  112.  
  113.  
  114.         /*
  115.         Increase SB Pro main and vol volumes to max
  116.         */
  117.  
  118.         if (devID == 5) {
  119.             SPP.Func = SetVolMainSBP;
  120.             SPP.Volume = 0x0F0F;
  121.             rez2 = RUCKDAC(&SPP);
  122.             SPP.Func = SetVolVocSBP;
  123.             SPP.VolVoc = 0x0F0F;
  124.             rez2 = RUCKDAC(&SPP);
  125.         }
  126.     }
  127.     return(rez);
  128. }
  129.  
  130.  
  131. int main()
  132. {
  133.  
  134.     int devID=-1, XMSflag = 0;
  135.  
  136.     printf("X01.C - RUCKUS-DAC play of VOC or WAVE file example. [930228]\n");
  137.  
  138.     rez = pick_device(&devID, &XMSflag);
  139.     if (devID >= 0) {
  140.         printf("Initializing devID %u\n",devID);
  141.         rez = init_device(devID);
  142.  
  143.         /*
  144.         The following load and play example source is coded inline here
  145.         to simply readability -- but it's so easy to add things that I just
  146.         kept adding stuff, so take it slow if you don't follow at first
  147.         */
  148.  
  149.         if (rez == 0) {
  150.             /* load file and setup playback parameters */
  151.  
  152.             printf("\nVOC/WAVE filename: ");
  153.             gets(filename);
  154.  
  155.             LP.Func = LoadDac;
  156.             LP.FilenamePtr = filename;
  157.             LP.StartPos = 0L;           /* start at first byte */
  158.             LP.LoadSize = 0L;           /* autoload entire file */
  159.             LP.XMMflag = XMSflag;
  160.             rez = RUCKDAC(&LP);
  161.             if (rez == 0) {
  162.                 /*
  163.                 Immediately after load, but before play (if non-DMA), we can
  164.                 peek into the DAC data to get the file's recorded sample rate
  165.                 */
  166.  
  167.                 printf(" Sample rate: %u\n",DACDATA.SampleRate);
  168.                 printf(" File format: ");
  169.                 if (DACDATA.Stereo != 0)
  170.                     printf("stereo ");
  171.                 else
  172.                     printf("mono ");
  173.                 if (DACDATA.Type == 1)
  174.                     printf("VOC\n");
  175.                 else
  176.                     printf("WAV\n");
  177.  
  178.                 /*
  179.                 Data is loaded, if device is a Sound Blaster use DMA at
  180.                 file sample rate else set rate to either file sample rate or,
  181.                 if > 11025 (most ATs can handle 11kHz in non-DMA mode) then
  182.                 set to 8000 Hz
  183.                 */
  184.  
  185.                 PBP.Func = PlayDac;
  186.                 if (devID >= 4)
  187.                     PBP.Mode = 2;
  188.                 else {
  189.                     PBP.Mode = 1;
  190.  
  191.                     /*
  192.                     Non-DMA mode needs to be set to a specific playback rate.
  193.  
  194.                     To play hal.voc using PCSPKR1 (the hi-rez mode) set
  195.                     SP.IntRate=8463. This results in upsample rate of 17045Hz
  196.                     */
  197.  
  198.                     if (DACDATA.SampleRate < 11025)
  199.                         SP.IntRate = DACDATA.SampleRate;
  200.                     else
  201.                         SP.IntRate = 8000;
  202.  
  203.                     SP.Func = SetIntRateDac;
  204.                     rez = RUCKDAC(&SP); /* set the playback rate */
  205.                     /* should check for errors in production code*/
  206.                 }
  207.  
  208.                 /*
  209.                 Load the file to DOS RAM or XMS
  210.                 */
  211.  
  212.                 if (LP.XMMflag == 0) {
  213.                     PBP.XMMhandle = 0;
  214.                     PBP.LoadPtr = LP.LoadPtr;
  215.                 }
  216.                 else {
  217.                     PBP.XMMhandle = LP.XMMhandle;
  218.                     PBP.LoadPtr = NULL;
  219.                 }
  220.                 rez = RUCKDAC(&PBP);
  221.  
  222.                 /*
  223.                 Playing in the background, wait until down or key pressed.
  224.                 To check if data done playing, read DACDATA.End. If non-zero
  225.                 then done playing. Note that once play has actually begun,
  226.                 the DACDATA.SampleRate is set to the actual playback rate,
  227.                 not necessarily the same as the rate at load time.
  228.                 */
  229.  
  230.                 if (rez == 0) {
  231.                     printf("   Device ID: %u\n",devID);
  232.                     printf("   Play rate: %u Hz\n",DACDATA.SampleRate);
  233.  
  234.                     if (LP.XMMflag == 0) {
  235.                         printf(" Memory type: DOS\n");
  236.                         printf("K bytes left: %u\n",DACDATA.MemDOS);
  237.                         printf("K bytes used: %u\n",DACDATA.MemUsed);
  238.                         printf("Load address: %Fp\n",LP.LoadPtr);
  239.                     }
  240.                     else {
  241.                         printf(" Memory type: XMS\n");
  242.                         printf("K bytes left: %u\n",DACDATA.MemXMM);
  243.                         printf("K bytes used: %u\n",DACDATA.MemUsed);
  244.                         printf("  XMS handle: %u\n",LP.XMMhandle);
  245.                     }
  246.  
  247.  
  248.                     GDP.Func = GetBytePosDac;
  249.                     do {
  250.                         rez = RUCKDAC(&GDP);
  251.                         if (GDP.BytePos != 0)
  252.                             printf("Current byte: %LX\r",GDP.BytePos);
  253.                     }
  254.                     while (DACDATA.End == 0);
  255.                     puts("");
  256.  
  257.  
  258.                     /*
  259.                     End play
  260.